package com.sx.medicinecard.utils;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.exception.LogException;
import com.sx.medicinecard.common.MyThreadPoolSingleton;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Component
public class AliLogUtils {

    /**
     * 阿里云 accessKeyId
     */
    @Value("${ali.sms.accessKeyId}")
    private  String accessKeyId;

    /**
     * 阿里云 secret
     */
    @Value("${ali.sms.accessKeySecret}")
    private  String accessKeySecret;

    /**
     * 日志服务的服务接入点。此处以杭州为例，其它地域请根据实际情况填写。
     */
    @Value("${ali.log.host}")
    private  String host;
    /**
     * 创建日志服务Client。
     */
     //private static Client client = new Client(host, accessKeyId, accessKeySecret);
     private static Client client;
    /**
     * // Project名称。
     */
    @Value("${ali.log.projectName}")
    private  String projectNa;
    private static String projectName;
    /**
     * Logstore名称。
     */
    @Value("${ali.log.logstoreName}")
    private String logstoreNa;

    private static String logstoreName;
    /**
     * 查询语句。
     */
    static String query = "*| select * from " + logstoreName;

    @EventListener(ContextRefreshedEvent.class)
    public void onContextRefreshed() {
        try {
            client = new Client(host, accessKeyId, accessKeySecret);
            this.projectName = this.projectNa;
            this.logstoreName = this.logstoreNa;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送信息到阿里日志
     * @param msg       消息
     * @param content   内容
     * @throws LogException
     */
    public static void pushLogToAli(String msg,String content) throws LogException, InterruptedException {
        List<LogItem> logGroup = new ArrayList<LogItem>();
        LogItem logItem = new LogItem();
        logItem.PushBack(msg,content);
        logGroup.add(logItem);
        client.PutLogs(projectName, logstoreName, "", logGroup, "");
        TimeUnit.SECONDS.sleep(5);
    }

    public static void pushLogToAli(String address,String requestBody,String responseBody) throws LogException, InterruptedException {
        List<LogItem> logGroup = new ArrayList<LogItem>();
        LogItem logItem = new LogItem();
        logItem.PushBack("请求接口地址",address);
        logItem.PushBack("请求参数",requestBody);
        logItem.PushBack("返回参数",responseBody);
        logGroup.add(logItem);
        client.PutLogs(projectName, logstoreName, "", logGroup, "");
        TimeUnit.SECONDS.sleep(5);
    }

    @Async("asyncExecutor")
    public void pushLogToAli(String address,String url,String phoneNo,String type,String requestBody,String responseBody){
        List<LogItem> logGroup = new ArrayList<LogItem>();
        LogItem logItem = new LogItem();
        logItem.PushBack("请求接口地址",address);
        logItem.PushBack("请求接口路径",url);
        logItem.PushBack("请求人账号",phoneNo);
        logItem.PushBack("日志类型",type);
        logItem.PushBack("请求参数",requestBody);
        logItem.PushBack("返回参数",responseBody);
        logGroup.add(logItem);
        try {
            client.PutLogs(projectName, logstoreName, "", logGroup, "");
            TimeUnit.SECONDS.sleep(5);
        } catch (LogException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
